package fragments;


import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.administrator.baseproject.BaseFragment;
import com.example.administrator.newxlmedical.R;

import beans.RegisterInfoBean;
import constants.Constants;
import constants.HandlerConstant;
import customhandler.MyHandler;
import singleton.SingletonClass;
import utils.ActivityUtil;
import utils.FormateUtils;
import utils.MediaPlayerUtils;
import utils.ToastUtil;

/**
 * A simple {@link Fragment} subclass.
 */
public abstract class BaseFingerprintRegisterFragment extends BaseFragment {
    public RegisterInfoBean bean;
    public MyHandler allHandler;
    public MediaPlayer mediaPlayer;

    public BaseFingerprintRegisterFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_fingerprint_register, container, false);
    }

    @Override
    protected void initView() {
        super.initView();
        SharedPreferences sharedPreferences = ActivityUtil.getSharedPreferences(mContext);
        allHandler = SingletonClass.getSingleton(MyHandler.class);
        bean = (RegisterInfoBean) getArguments().getSerializable(Constants.BASE_FINGERPRINT_REGISTER_FRAGMENT);
        allHandler.sendEmptyMessage(HandlerConstant.FINGERPRINT_REGISTER_FIRST);
        MediaPlayerUtils.playSound(mContext,R.raw.fingerprint_register_down);
    }

    @Override
    protected void onHandleMessage(Message msg) {
        super.onHandleMessage(msg);
        switch (msg.what) {
            case HandlerConstant.FINGERPRINT_REGISTER_FIRST_RETURN:
                allHandler.sendEmptyMessage(HandlerConstant.FINGERPRINT_REGISTER_SECOND);
                break;
            case HandlerConstant.FINGERPRINT_REGISTER_SECOND:

                break;
            case HandlerConstant.FINGERPRINT_REGISTER_SECOND_RETURN:
                allHandler.sendEmptyMessage(HandlerConstant.FINGERPRINT_REGISTER_THIRD);

                break;
            case HandlerConstant.FINGERPRINT_REGISTER_THIRD:

                break;
            case HandlerConstant.FINGERPRINT_REGISTER_THIRD_RETURN:

//                allHandler.sendEmptyMessage(HandlerConstant.FINGERPRINT_REGISTER_THIRD);
                break;
            case HandlerConstant.FINGERPRINT_REGISTER_ERROR:
                ToastUtil.showToast(mContext, "注册失败");
                resetFingerprint();
                break;
            case HandlerConstant.FINGERPRINT_FEATURE_SUCCESS:
                String s = (String) msg.obj;
                String fingerprintUserId = s.substring(0, 4);
                String fingerprintFeature = s.substring(6, s.length());

                netWork(fingerprintUserId, fingerprintFeature);
                break;
//            case HandlerConstant.DELETE_FINGERPRINT_USER_SUCCESS:
//                allHandler.sendEmptyMessage(HandlerConstant.REGISTER_SUCCESS);
//                break;
//                case HandlerConstant.TRANSFER_FINGERPRINT_FEATURE_SUCCESS:
//                    myFingerprintDialog.dismiss();
//                    ActivityUtil.toastShow(mContext, "注册成功");
//                    myRegisterSuccessDialog.show();
//                    mediaPlayer.release();
//                    mp = MediaPlayer.create(mContext, R.raw.register_success);
//                    mp.start();
//                    break;

            case HandlerConstant.ACK_FAIL:
                ToastUtil.showToast(mContext, "执行失败");
                resetFingerprint();
                break;
            case HandlerConstant.ACK_FULL:
                ToastUtil.showToast(mContext, "数据库已满");
                resetFingerprint();
                break;
            case HandlerConstant.ACK_TIMEOUT:
                ToastUtil.showToast(mContext, "指纹输入超时");
                resetFingerprint();
                break;
//                case HandlerConstant.ACK_BREAK:
//                    ActivityUtil.toastShow(mContext, "当前命令已被终止");
//                    resetFingerprintDialog();
//                    break;
            case HandlerConstant.ACK_IMAGEFIAL:
                ToastUtil.showToast(mContext, "图像不合格");
                break;
        }
    }

    private void resetFingerprint() {
        allHandler.sendEmptyMessage(HandlerConstant.FINGERPRINT_REGISTER_FIRST);
    }

    abstract void netWork(String fingerprintUserId, String fingerprintFeature);
}
